<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/13
 * Time: 15:51
 */

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Index_MxShopPage extends WebPage {

    public function main() {
        global $_W, $_GPC;

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

//        $status = intval($_GPC['status']) == 2 ? 0 : 1;
        $status = intval($_GPC['status']);

        //获取网关平台信息
        $res = m('platform')->getList($pindex, $psize, $status);
        if($res['code'] != 0){
            $this->message($res['msg']);
        }
        $list = $res['data']['list'];
        if(!empty($list))
        {
            foreach ($list as $key=>$item)
            {
                $list[$key]['uni'] = pdo_fetchcolumn('SELECT name FROM '.tablename('uni_account').' WHERE uniacid=:uniacid', array(':uniacid'=>$item['uniacid']));
            }
        }

        $pager = pagination($res['data']['total'], $pindex, $psize);

        include $this->template();
    }

    public function post(){

        global $_W, $_GPC;

        $id = intval($_GPC ['id']);

        if (!empty ($id)) {
            $item = pdo_fetch("SELECT * FROM " . tablename('mx_shop_platform') . " WHERE platformid=:platformid", array('platformid'=>$id));

        } else {
            $item = array(
                'displayorder' => 0,
                'thumb' => '',
                'status' => 1
            );
        }

        if ($_W['ispost']) {

            if (empty ($_GPC ['catename'])) {
                show_json(0, '抱歉，请输入平台名称！');
            }

            $mcname = pdo_fetch("SELECT id FROM " . tablename('mx_shop_platform') . " WHERE platformid != :platformid AND name = :mcname AND deleted=0", array(':platformid' => $id, ':mcname' => $_GPC['catename']));
            if(!(empty($mcname['id']))){
                show_json(0,"该平台名称已存在");
            }

            $uniacid = intval($_GPC['uniacid']);
            if(empty($uniacid)){
                show_json(0, '请选择平台对应的公众号');
            }
            $platform = pdo_fetch("SELECT id FROM " . tablename('mx_shop_platform') . " WHERE uniacid=:uniacid AND platformid != :platformid AND deleted=0", array(':uniacid' => $uniacid, ':platformid' => $id));
            if(!empty($platform)){
                show_json(0, '该公众号已有对应平台');
            }

            $data = array(
                'uniacid' => $uniacid,
                'name' => $_GPC ['catename'],
                'description' => empty($_GPC ['description']) ? ' ' : $_GPC ['description'],
                'status' => intval($_GPC ['status']),
                'thumb' => $_GPC ['thumb'],
                'displayorder' => intval($_GPC ['displayorder']),
                'updatetime' => TIMESTAMP
            );


            if (!empty ($id))
            {
                if(empty($item['platform']))
                {
                    //添加到网关
                    $res = m('platform')->addPlatformToProxy($data);
                    $data['platformid'] = intval($res['data']['platform']);

                }else
                {
                    //修改网关信息
                    $res = m('platform')->editPlatformToProxy($item['platformid'], $data);
                }
                if($res['code'] != 0){
                    show_json(0, $res['msg']);
                }

                if($item['status'] != $data['status']){
                    //修改网关平台状态
                    $res = m('platform')->editPlatformStatusToProxy($item['platformid'], $data['status']);
                    if($res['code'] != 0){
                        show_json(0, $res['msg']);
                    }
                }

                //更新本地信息
                pdo_update('mx_shop_platform', $data, array('platformid' => $id));
                plog('platform.add', '更新"' . $data['name'] . '"平台成功！平台ID为' . $id);
            }
            else
            {
                //添加到网关
                $res = m('platform')->addPlatformToProxy($data);
                if($res['code'] != 0){
                    show_json(0, $res['msg']);
                }

                //保存到本地
                $data['platformid'] = intval($res['data']['platform']);
                $data['createtime'] = TIMESTAMP;
                pdo_insert('mx_shop_platform', $data);
                $id = pdo_insertid();
                plog('platform.add', '新建"' . $data['name'] . '"平台成功！平台ID为' . $id);
            }
            show_json(1, array('url'=>webUrl('platform')));

        }

        //获取所有的公众号信息
        $uni = pdo_fetchall('SELECT uniacid,name,groupid FROM '.tablename('uni_account'));

        include $this->template();
    }

    public function status(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        $status = intval($_GPC['status']);

        if(empty($id)){
            show_json(0, '参数有误');
        }

        //修改网关平台状态
        $res = m('platform')->editPlatformStatusToProxy($id, $status);
        if($res['code'] != 0){
            show_json(0, $res['msg']);
        }

        if($status == 1)
        {
            pdo_update('mx_shop_platform', array('status'=>1), array('platformid'=>$id));

        }else
        {
            pdo_update('mx_shop_platform', array('status'=>0), array('platformid'=>$id));
        }

        show_json(1, array('url'=>webUrl('platform')));

    }
}
